package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;



import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;




import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;


import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;




import java.util.*;


import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 体检预约服务
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    //体检预约
    public Result order(Map map) throws Exception {
        /**
         1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
         2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
         3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
         4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
         5、预约成功，更新当日的已预约人数
         */

        //获取日期
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if(orderSetting == null){
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断是否在重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member != null){
            Integer memberId = member.getId();//会员id
            Integer setmealId = Integer.parseInt((String)map.get("setmealId"));//套餐id
            Order order = new Order(memberId,date,setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if(orderList != null && orderList.size() > 0){
                //用户在重复预约，不能完成预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        if(member == null){
            //当前用户不是会员，需要自动完成注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //保存预约信息
        Order order = new Order(member.getId(),
                date,
                (String)map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据id查询预约详细信息（包括会员姓名、套餐名称、预约基本信息）
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate",DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }




    @Override
    public PageResult findOrdersPage(QueryPageBean queryPageBean) throws Exception {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        Map queryStrings = queryPageBean.getQueryStrings();
        String queryString = (String) queryStrings.get("queryString");
        String orderStatus = (String) queryStrings.get("orderStatus");
        String orderType = (String) queryStrings.get("orderType");
        List<String> list = (List<String>) queryStrings.get("orderDate");

        Map<String, Object> map = new HashMap<>();
        Date beforeDate = null;
        Date afterDate = null;
        if (list != null && list.size() > 0){
            String date1 = list.get(0);
            String date2 = list.get(1);
            beforeDate = DateUtils.parseString2Date(date1);

            afterDate = DateUtils.parseString2Date(date2);

        }

            map.put("queryString",queryString);
            map.put("orderStatus",orderStatus);
            map.put("orderType",orderType);
            map.put("beforeDate",beforeDate);
            map.put("afterDate",afterDate);
            PageHelper.startPage(currentPage,pageSize);
            Page<Map> page = orderDao.findOrdersPage(map);
            long total = page.getTotal();
            List<Map> result = page.getResult();

            return new PageResult(total,result);



    }




    @Override
    public Result submit(Map map, Integer[] setmealIds) throws Exception {

        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if(orderSetting == null){
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断是否在重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member != null){
            Integer memberId = member.getId();//会员id
            if (setmealIds.length == 0){
                return new Result(false, MessageConstant.ADD_Undefined_FAIL);
            }
            Integer setmealId = setmealIds[0];
            Order order = new Order(memberId,date,setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if(orderList != null && orderList.size() > 0){
                //用户在重复预约，不能完成预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        if (member == null){
            String name = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            String birthday = (String) map.get("birthday");
            Date date1 = DateUtils.parseString2Date(birthday);
            String age = (String) map.get("age");
            String sex = (String) map.get("sex");

            member = new Member();

            member.setName(name);
            member.setIdCard(idCard);
            member.setBirthday(date1);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            memberDao.add(member);
        }
        Integer memberId = member.getId();
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(date);
        order.setOrderStatus("未到诊");
        order.setOrderType("电话预约");
        if (setmealIds.length == 0){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        Integer setmealId = setmealIds[0];
        order.setSetmealId(setmealId);
        orderDao.add(order);
        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }

    @Override
    public void delete(Integer id) {
        orderDao.delete(id);
    }

    @Override
    public Result edit(Map map, Integer[] setmealIds) throws Exception {
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if(orderSetting == null){
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断是否在重复预约
        String telephone = (String) map.get("phoneNumber");
        Member member = memberDao.findByTelephone(telephone);
        System.out.println(member);
        if(member != null){
            if (setmealIds.length == 0){
                return new Result(false, MessageConstant.ADD_Undefined_FAIL);
            }
            Integer setmealId = setmealIds[0];
            Integer memberId = member.getId();//会员id
            Order order = new Order(memberId,date,setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if(orderList != null && orderList.size() > 0){
                //用户在重复预约，不能完成预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        if (member == null){
            String name = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            String birthday = (String) map.get("birthday");
            Date date1 = DateUtils.parseString2Date(birthday);
            String age = (String) map.get("age");
            String sex = (String) map.get("sex");

            member = new Member();

            member.setName(name);
            member.setIdCard(idCard);
            member.setBirthday(date1);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            memberDao.add(member);
        }




        Integer memberId = member.getId();
        Integer id = (Integer) map.get("id");
        Order order = new Order();
        order.setId(id);
        order.setMemberId(memberId);
        order.setOrderDate(date);
        order.setOrderStatus("未到诊");
        order.setOrderType("电话预约");
        if (setmealIds.length == 0){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        Integer setmealId = setmealIds[0];
        order.setSetmealId(setmealId);
        orderDao.edit(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }

    @Override
    public void switchStatusChange(Map map) {
        int id = (int) map.get("id");

        String orderStatus = null;
        boolean flag = (boolean) map.get("value");
        if (flag) {
            orderStatus = "已到诊";
        } else {
            orderStatus = "未到诊";
        }
        orderDao.switchStatusChange(id,orderStatus);
    }
}
